// merchants/qrCode/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrCode: '',
    couponInfo: {},
    orderamountlower: 0,
    money: 0,
    name: '',
    extcode1: '',
    extcode2: '',
    distance: 0,
    couponsn: 0,
    check: 0,
    timer: '',
    timeList: [],
    companyInfo: {},
    province: ''
  },

  getQrcode: function(couponid, coupontypeid, creatuid) {
    var that = this;
    let cardid = wx.getStorageSync("cardId");
    let path = '';
    path = 'merchant/share/toVerifications/index?p=' + app.globalData.userInfo.Uid + '#' + coupontypeid + '#' + couponid + '#' + cardid;
    wx.request({
      url: app.globalData.host + "/api/Store/GetWxaCodeFile",
      data: {
        storeId: app.globalData.storeId,
        path: path,
        storeappid: app.globalData.storeAppId,
        type: "coupon"
      },
      success: function(res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            that.setData({
              qrCode: 'https://static.711688.net.cn/upload/couponcode/' + res.data.Data
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      },
    })
  },
  getShip: function (uid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Ship/GetShip?uid=' + uid,
      success: function (res) {
        if (res.data.Code == 0) {
          if (res.data.Data) {
            if (res.data.Data.WorkTime) {
              let timeList = JSON.parse(res.data.Data.WorkTime);
              that.setData({
                timeList: timeList
              })
            }
          }
        }
      }
    })
  },

  getInfo: function(uid){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Staff/GetStaffIdByUid?Uid=' + uid,
      success: function(res){
        if(res.data.Code == 0){
          if(res.data.Data){
            that.getProvince(res.data.Data.Regionid)
            that.setData({
              companyInfo: res.data.Data
            })
          }
        }
      }
    })

  },
  getProvince: function(regionid){
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Tool/RegionListByIds?regionIds=' + regionid,
      success: function(res){
        if(res.data.Code == 0){
          let province = res.data.Data[0].ProvinceName + res.data.Data[0].CityName + res.data.Data[0].Name
          that.setData({
            province: province
          })
        }
      },
    })
  },
  //地图导航
  openMapLocation: function () {
    var that = this;
    var cardInfo = that.data.companyInfo;
    wx.showModal({
      title: '提示',
      content: '是否打开地图导航',
      success: function (res) {
        if (res.confirm) {
          wx.getLocation({
            type: 'gcj02',
            success: function (res) {
              wx.openLocation({
                latitude: cardInfo.Latitude,
                longitude: cardInfo.Longitude,
                scale: 28,
                name: "点击右边打开地图导航",
                address: cardInfo.Address
              })
            }
          })
        }
      }
    })
  },


  // 刷新小程序码状态
  freshCode: function(couponid) {
    let that = this;
    var timesRun = 0;
    var timer = setInterval(function() {
      timesRun += 1;
      if (timesRun === 20) {
        clearInterval(timer);
      }
      wx.request({
        url: app.globalData.host + '/api/coupon/ischeck?couponId=' + couponid + '&uid=' + app.globalData.userInfo.Uid,
        success: function(res) {
          if (res.data.Code == 0) {
            if (res.data.Data != null) {
              that.setData({
                check: 1
              })
              clearInterval(timer);
              setTimeout(function(){
                let createUid = that.data.couponInfo.CreateUid;
                wx.request({
                  url: app.globalData.host + '/api/Alliance/GetAllianceListByApply?uid=' + createUid,
                  success: function (res) {
                    if (res.data.Code == 0) {
                      let addedList = res.data.Data.AllianceList;
                      if (addedList.length > 0) {
                        that.getAllianRecord(addedList[0].Id);
                      }
                    } else {
                      wx.showModal({
                        title: '提示',
                        content: res.data.Message,
                        showCancel: false
                      })
                    }
                  }
                })
                
              },3000);
            }
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.Message,
              showCancel: false
            })
          }
        }
      })
    }, 3000)
    that.setData({
      timer: timer
    })
  },
  getAllianRecord: function(allianceId){
    let cardid = wx.getStorageSync("cardId");
    wx.request({
      url: app.globalData.host + '/api/alliance/GetPackageRecord?allianceid='+ allianceId +'&uid=' + app.globalData.userInfo.Uid,
      success: function(res){
        if(res.data.Code == 0){
          if(!res.data.Data){
            wx.redirectTo({
              url: '/merchant/alliance/receiveAllianceCoupon/index?allianceid='+ allianceId + '&id=' + cardid
            })
          }
        }else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      } 
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let orderamountlower = options.orderamountlower;
    let name = options.name;
    let money = options.money;
    let extcode1 = options.extcode1;
    let extcode2 = options.extcode2;
    let couponid = options.couponid;
    let distance = options.distance;
    this.setData({
      name: name,
      orderamountlower: orderamountlower,
      money: money,
      extcode1: extcode1,
      extcode2: extcode2,
      distance: distance
    })
    this.getCouponInfo(couponid);
    this.freshCode(couponid);
  },
  getCouponInfo: function(couponid) {
    let that = this;
    wx.request({
      url: app.globalData.host + '/api/Coupon/GetCouponInfo?couponId=' + couponid + '&uid=' + app.globalData.userInfo.Uid,
      success: function(res) {
        if (res.data.Code == 0) {
          let couponInfo = res.data.Data.Coupon
          that.getQrcode(couponid, couponInfo.CouponTypeId, couponInfo.CreateUid);
          that.getShip(couponInfo.CreateUid);
          that.getInfo(couponInfo.CreateUid);
          that.setData({
            couponInfo: couponInfo
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  onUnload: function() {
    clearInterval(this.data.timer);
  }

})